﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Net;

namespace WFAExamAuto.SysManage
{
    
    public partial class Subject : Form
    {
        string subject;
        public FrmMain fmain = new FrmMain();
        public Subject()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void Subject_Load(object sender, EventArgs e)
        {
            LoadSubject();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            fmain.cps.Pro_DelSubject(dgvSubjectMan[0, dgvSubjectMan.CurrentRow.Index].Value.ToString());
            LoadSubject();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string str;
              //  StringBuilder str = new StringBuilder();
                str=dgvSubjectMan[0, dgvSubjectMan.RowCount - 2].Value.ToString();
                fmain.cps.Pro_AddSubject(str, Properties.Settings.Default.UserName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            LoadSubject();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadSubject();
        }
        /// <summary>
        /// 加载数据到datagridview中
        /// </summary>
        public void LoadSubject()
        {
            try
            {
                dgvSubjectMan.AutoGenerateColumns = false;
                dgvSubjectMan.DataSource = fmain.cps.Pro_GetAllSubject();
                dgvSubjectMan.Columns[0].DataPropertyName = "subject_name";
                dgvSubjectMan.Columns[1].DataPropertyName = "SubjectUserName";
            }
            catch
            { 
            
            }
        }

        private void dgvSubjectMan_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            //dgvSubjectMan.SelectedRows[1].SetValues(Properties.Settings.Default.UserName);
            subject = dgvSubjectMan[0, e.RowIndex].Value.ToString();
        }

        private void dgvSubjectMan_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.RowIndex > -1)
            {
                dgvSubjectMan[1, e.RowIndex].Value = Properties.Settings.Default.UserName;
                
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            fmain.cps.Pro_UpdateSubject(subject,dgvSubjectMan[0,dgvSubjectMan.CurrentRow.Index].Value.ToString());
            LoadSubject();
        }

        private void dgvSubjectMan_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //if (dgvSubjectMan[0, dgvSubjectMan.CurrentRow.Index].Value!= null)
            //{
            //    if (fmain.cps.Pro_UpdateSubject(subject, dgvSubjectMan[0, dgvSubjectMan.CurrentRow.Index].Value.ToString())== 0)
            //    {
            //        btnAdd_Click(sender, e);
            //    }
            //}
            fmain.cps.Pro_UpdateSubject(subject, dgvSubjectMan[0, dgvSubjectMan.CurrentRow.Index].Value.ToString());
            LoadSubject();
        }

        private void dgvSubjectMan_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (dgvSubjectMan[0, e.RowIndex].Value == null)
            {
                MessageBox.Show("科目名称不能为空！");
                dgvSubjectMan[0, e.RowIndex].Value = subject;
            }
        }

        private void dgvSubjectMan_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
//            dgvSubjectMan_CellValueChanged(sender, e);
              dgvSubjectMan_CellEndEdit(sender, e);

        }
    }
}
